package com.windchime.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.windchime.blog.entity.Resource;
import com.windchime.blog.entity.ResourceType;
import com.windchime.blog.service.ResourceService;
import com.windchime.blog.service.ResourceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.Map;

@Controller
public class FrontResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private ResourceTypeService resourceTypeService;

    /**
     * 资源页
     * @param model 传值
     * @return index
     */
    @GetMapping("/resources")
    public String resources(Model model) {
        //资源分类的展示
        List<ResourceType> resourceTypes = resourceTypeService.list(new QueryWrapper<ResourceType>().eq("flag", false).eq("parent_id", 0));
        for (ResourceType resourceType : resourceTypes) {
            Integer oneTypeHasResources = resourceService.selectOneTypeHasResources(resourceType.getId());
            resourceType.setParentHasResources(oneTypeHasResources);
        }
        model.addAttribute("resourceTypes", resourceTypes);

        //资源展示--以资源分类为组
        Map<String, List<Resource>> resourceSplit = resourceService.resourceSplit();
        model.addAttribute("resources", resourceSplit);

        return "/resources";
    }

}
